package gui.newgui.newtournamentwindow;

import gui.helper.CorrectnessTester;
import gui.newgui.IController;
import gui.newgui.mainwindow.MainWindow;

import java.io.File;
import java.io.IOException;

import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

import org.xml.sax.SAXException;

import tournamentData.GeneralTournamentInformation;
import tournamentData.TournamentData;
import core.CustomIcons;
import core.Date;
import core.XMLWriter;
import db.newversion.DbEntry;
import db.newversion.DistancesDB;

public class NewTournamentWindowController implements IController {
	private final NewTournamentWindow2 window;
	private boolean hasFormErrors;

	public NewTournamentWindowController(NewTournamentWindow2 newTournamentWindow) {
		this.window = newTournamentWindow;
		this.hasFormErrors = false;
	}

	@Override
	public void updateTable() {
		// TODO Auto-generated method stub

	}

	protected void setTournementDropDownEntries() {
		ComboBoxModel model = new DefaultComboBoxModel(DistancesDB.getInstance().getAll(DbEntry.TOURNAMENT_TYPE));
		this.window.getTournamentTypeDropDown().setModel(model);
	}

	protected void setCurrentDate() {
		this.window.getStartDateTextField().setText(Date.getCurrentDate().toString());
		this.window.getEndDateTextField().setText(Date.getCurrentDate().toString());
	}

	private void hasFormErrors() {
		this.hasFormErrors = false;

		/** tournament type error handling */
		try {
			this.hasFormErrors = CorrectnessTester.testCorrectness(this.hasFormErrors, this.window.getTournamentTypeDropDown().getSelectedItem().toString().isEmpty(),
					this.window.getTournamentTypeLabel());
		} catch (NullPointerException e) {
			this.hasFormErrors = CorrectnessTester.testCorrectness(this.hasFormErrors, true, this.window.getTournamentTypeLabel());
		}

		/** tournament name error handling */
		this.hasFormErrors = CorrectnessTester.testCorrectness(this.hasFormErrors, this.window.getTournamentNameTextField().getText().isEmpty(), this.window.getTournamentNameLabel());

		/** start-date error handling */
		this.window.getStartDateTextField().setText(new Date(this.window.getStartDateTextField().getText()).toString());
		this.hasFormErrors = CorrectnessTester.testCorrectness(this.hasFormErrors, new Date(this.window.getStartDateTextField().getText()).equals(new Date("some error generating format")),
				this.window.getStartdateLabel());

		/** end-date error handling */
		this.window.getEndDateTextField().setText(new Date(this.window.getEndDateTextField().getText()).toString());
		this.hasFormErrors = CorrectnessTester.testCorrectness(this.hasFormErrors, new Date(this.window.getEndDateTextField().getText()).equals(new Date("some error generating format")),
				this.window.getEndDateLabel());

		/** organizer error handling */
		this.hasFormErrors = CorrectnessTester.testCorrectness(this.hasFormErrors, this.window.getOrganizerTextField().getText().isEmpty(), this.window.getOrganizerLabel());

		/** tournament lead error handling */
		this.hasFormErrors = CorrectnessTester.testCorrectness(this.hasFormErrors, this.window.getTournamentLeadTextField().getText().isEmpty(), this.window.getTournamentLeadLabel());

		/** referee error handling */
		this.hasFormErrors = CorrectnessTester.testCorrectness(this.hasFormErrors, this.window.getRefereeTextField().getText().isEmpty(), this.window.getRefereeLabel());
	}

	protected void createNewTournament() {
		this.hasFormErrors();

		if (!this.hasFormErrors) {
			this.window.getThrobberLabel().setIcon(CustomIcons.throbber);
			/**
			 * read entered information
			 */
			String tournamentType = this.window.getTournamentTypeDropDown().getSelectedItem().toString();
			String tournamentName = this.window.getTournamentNameTextField().getText();
			Date startDate = new Date(this.window.getStartDateTextField().getText());
			Date endDate = new Date(this.window.getEndDateTextField().getText());
			String organizer = this.window.getOrganizerTextField().getText();
			String tournamentLead = this.window.getTournamentLeadTextField().getText();
			String referee = this.window.getRefereeTextField().getText();

			GeneralTournamentInformation generalInformation = new GeneralTournamentInformation(tournamentType, tournamentName, startDate, endDate, organizer, tournamentLead, referee);
			TournamentData.getInstance().setGeneralTournamentInformation(generalInformation);

			/** save tournament */
			try {
				XMLWriter.writeToFile(new File(XMLWriter.getFileName(TournamentData.getInstance().getGeneralTournamentInformation().getTournamentName(), TournamentData.getInstance()
						.getGeneralTournamentInformation().getStartDate())));

			} catch (SAXException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			/** save tournamentType to DB */
			DistancesDB.getInstance().insertTournamentType(tournamentType);

			/** open the main Window */
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					MainWindow inst;
					File toOpen = new File(XMLWriter.getFileName(TournamentData.getInstance().getGeneralTournamentInformation().getTournamentName(), TournamentData.getInstance()
							.getGeneralTournamentInformation().getStartDate()));
					inst = new MainWindow(toOpen);
					inst.setLocationRelativeTo(null);
					inst.setVisible(true);
				}
			});
			this.window.getParentWindow().dispose();
			this.window.dispose();

		} else {
			JOptionPane.showMessageDialog(null, "Eingabefehler");
		}
	}
}
